package com.roc.service.impl.settingImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.pojo.Operators;
import com.roc.service.setting.SettlementToPayAgainService;
import com.roc.util.MD5Utils;
import com.roc.util.constant.PayStatus;
import com.roc.util.constant.SysLogType;
@Service
@Transactional
public class SettlementToPayAgainServiceImpl implements
		SettlementToPayAgainService {
	
	@Autowired
	private IBaseDao baseDao;
	
	@Autowired
	ISQLBaseDao sqlBaseDao;
	
	@Override
	public void updateStatus(String payNo,Operators op) {
		try{
			StringBuilder str=new StringBuilder();//定义变长字符串
			Random random=new Random();
			//随机生成数字，并添加到字符串
			for(int i=0;i<8;i++){
			    str.append(random.nextInt(10));
			}
			String querySql = "select s.INS_ID_CD,s.SETTLEDATE,s.MCHT_CD,s.count from t_settle_item s where s.remark1=?";
			Object[] parmasQ=new Object[1];
			parmasQ[0] = payNo;
			Object[] object = (Object[])sqlBaseDao.findObj(querySql, parmasQ);
			String sql = "update t_settle_item s set s.settleflag=?,s.SERIAL_NO=?,s.count=? where s.remark1=?";
			List<Object> parmas=new ArrayList<Object>();
			String payAgainNo = object[0].toString()+MD5Utils.MD5Encode(object[1].toString()+object[2].toString()+str.toString());
			parmas.add(PayStatus.SETTLE_WAIT.getCode());
			parmas.add(payAgainNo);
			if(object[3]!=null&&object[3]!=""){
				parmas.add((Integer.parseInt(object[3].toString())+1));
			}else{
				parmas.add(1);
			}
			parmas.add(payNo);
			sqlBaseDao.deleteBySql(sql, parmas);
			baseDao.addSyslog(SysLogType.PAY_AGAIN.getCode(),
					SysLogType.PAY_AGAIN.getName(), op.getAccountNo(),
					"操作员"
							+ op.getAccountNo() + "将流水号【"
							+ payAgainNo + "】进行补付操作");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	@Override
	public List<Object> queryPayFailList(String payNo) {
		try{
			String sql = "select s.id,s.ins_id_cd,s.mcht_cd,s.mcht_name,s.trans_amt,s.total_num,s.mcht_fee,s.mcht_set_amt,s.bank_name,s.bank_code,s.account,s.account_name,s.acc_type,s.settledate,s.settleflag,s.setdate,s.freezeflag,s.dt_create,s.dt_update,s.freeze_amt,s.un_freeze_amt,s.remark1,s.remark,s.IS_COMBING,s.SERIAL_NO,s.COUNT,s.CHANGE_FLAG,s.REMARK5,REASON from T_SETTLE_ITEM_COMPENSATE s where s.remark1='"+payNo+"'";
			List<Object> objList = sqlBaseDao.queryAll(sql);
			return objList;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

}
